#include "../mqserver/queue.hpp"
#include <gtest/gtest.h>

zhong::MessageQueueManager::ptr queue_manager;

class QueueTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        queue_manager = std::make_shared<zhong::MessageQueueManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        queue_manager->clear();  // 清理数据库
    }
};
// 插入测试----已成功
TEST(queue_test, insert_test)
{
    std::unordered_map<std::string, std::string> map = {{"apple", "苹果"}, {"banana", "香蕉"}};
    queue_manager->declareQueue("queue1", true, true, false, map);
    queue_manager->declareQueue("queue2", true, true, false, map);
    queue_manager->declareQueue("queue3", true, true, false, map);
    queue_manager->declareQueue("queue4", true, true, false, map);
    ASSERT_EQ(queue_manager->size(), 4);
}
// 查询测试---已成功
TEST(queue_test, select_test)
{
    // 注意查询不存在的内容，会出现Segmentation fault (core dumped)

    zhong::MessageQueue::ptr queue = queue_manager->selectQueue("queue2");
    ASSERT_EQ(queue->_name, "queue2");
    ASSERT_EQ(queue->_durable, true);
    ASSERT_EQ(queue->_auto_delete, false);
    // ASSERT_EQ(queue->getArgs(), std::string("banana=香蕉&apple=苹果&"));
}
// 删除测试---已成功
TEST(queue_test, remove_test)
{
    queue_manager->deleteQueue("queue2");
    zhong::MessageQueue::ptr queue = queue_manager->selectQueue("queue2");
    ASSERT_EQ(queue.get(), nullptr);
    ASSERT_EQ(queue_manager->exist("queue2"), false); // 表示map中的queue2已经删除
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new QueueTest);
    RUN_ALL_TESTS();
}

